package com.fin.zw.aiqas.controller;

import com.fin.zw.aiqas.annotation.CurrentUser;
import com.fin.zw.aiqas.model.request.FormulaRequest;
import com.fin.zw.aiqas.model.request.RuleInfoKeywordRequest;
import com.fin.zw.aiqas.model.response.FormulaResponse;
import com.fin.zw.aiqas.model.session.SessionModel;
import com.fin.zw.aiqas.service.FormulaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

import static org.springframework.web.bind.annotation.RequestMethod.PATCH;
import static org.springframework.web.bind.annotation.RequestMethod.PUT;

@RestController
@RequestMapping("/formula")
@Api(value = "质检点信息维护", description = "质检点信息维护")
public class FormulaController {

    private final static Logger log = LoggerFactory.getLogger(FormulaController.class);

    @Autowired
    FormulaService formulaService;

    @PostMapping("/add")
    @ApiOperation(value = "添加质检点", notes = "添加质检点")
    public ResponseEntity<Void> addCalculation(@RequestBody RuleInfoKeywordRequest request, @ApiIgnore @CurrentUser SessionModel session) {
        formulaService.addCalculation(request, session);
        return ResponseEntity.ok().build();
    }

    @RequestMapping(value = "/update", method = {PUT, PATCH}, name = "updateCalculation")
    @ApiOperation(value = "修改质检点信息", notes = "修改质检点信息")
    public ResponseEntity<Void> updateCalculation(@RequestBody RuleInfoKeywordRequest request, @ApiIgnore @CurrentUser SessionModel session) {
        formulaService.updateCalculation(request, session);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除质检点信息", notes = "删除质检点信息")
    public ResponseEntity<Void> deleteCalculation(@PathVariable Long id) {
        formulaService.deleteCalculation(id);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/fetchPage")
    @ApiOperation(value = "分页查询质检点信息", notes = "分页查询质检点信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", dataType = "int", paramType = "query", value = "页数（0..N）"),
            @ApiImplicitParam(name = "size", dataType = "int", paramType = "query", value = "每页大小."),
            @ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query", value = "依据什么排序: 属性名(asc|desc). "),
    })
    public ResponseEntity<Page<FormulaResponse>> queryAllCalculationForPage(FormulaRequest request, Pageable pageable) {
        Page<FormulaResponse> list = formulaService.queryAllCalculation(request, pageable);
        return ResponseEntity.ok().body(list);
    }


    @GetMapping("/fetch")
    @ApiOperation(value = "查询质检点信息", notes = "查询质检点信息")
    public ResponseEntity<List<FormulaResponse>> queryAllCalculation(FormulaRequest request) {
        List<FormulaResponse> list = formulaService.getAllCalculation(request);
        return ResponseEntity.ok().body(list);
    }
}
